package com.xuptGurantee.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuptGurantee.system.common.R;
import com.xuptGurantee.system.entity.Volunteer;
import com.xuptGurantee.system.service.VolunteerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {

    @Resource
    VolunteerService volunteerService;

    @PostMapping("/login")
    public R<Volunteer> loginStunum(HttpServletRequest request , @RequestBody Volunteer volunteer) {
        String password = volunteer.getVolunteerPass();
        LambdaQueryWrapper<Volunteer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Volunteer::getVolunteerStunum, volunteer.getVolunteerStunum());
        Volunteer volunteerServiceOne = volunteerService.getOne(queryWrapper);

        //3、如果没有查询到则返回登录失败结果
        if(volunteerServiceOne == null){
            return R.error("登录失败");
        }

        //4、密码比对，如果不一致则返回登录失败结果
        if(!volunteerServiceOne.getVolunteerPass().equals(password)){
            return R.error("登录失败");
        }

        //6、登录成功，将员工id存入Session并返回登录成功结果
        request.getSession().setAttribute("normalUser",volunteerServiceOne.getId());
        return R.success(volunteerServiceOne);
    }

}


